package com.bailiban.search.controller;

import com.bailiban.common.entity.PageResult;
import com.bailiban.common.entity.Result;
import com.bailiban.common.entity.StatusCode;
import com.bailiban.search.entity.Article;
import com.bailiban.search.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhulang
 * @Classname ArticleController
 * @Description ArticleController
 */
@RestController
@RequestMapping("es/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return new Result(true, StatusCode.OK, "索引添加成功");
    }

    @GetMapping("search/keywords/{keywords}")
    public Result search(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "page", defaultValue = "5") int size, @PathVariable String keywords) {
        Page page1 = articleService.search(page, size, keywords);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Article>(page1.getTotalElements(), page1.getContent()));
    }

    @GetMapping("search/id/{id}")
    public Result findById(@PathVariable String id) {
        Article article = articleService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", article);
    }

    @GetMapping("search/hight/{keywords}")
    public Result getHight(@PathVariable String keywords) {
        List<Article> hight = articleService.getHight(keywords);
        return new Result(true, StatusCode.OK, "高亮结果查询成功", hight);
    }
}
